package com.itany.cloud.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author 石小俊
 */
//@Component
public class AuthenticationFilter implements GlobalFilter, Ordered {

    /**
     * 定义全局过滤器内容
     * @param exchange
     * @param chain
     * @return
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 获取请求参数
        String company = exchange.getRequest().getQueryParams().getFirst("company");
        if("itany".equals(company)){
            return chain.filter(exchange);
        }
        exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
        return exchange.getResponse().setComplete();
    }

    /**
     * 定义过滤器的优先级
     * @return  其值范围即为Integer类型数值的范围
     */
    @Override
    public int getOrder() {
        return 0;
    }
}
